package com.gitee.feizns.quickstart.ding.talk.client;

import com.aliyun.teaopenapi.Client;
import com.gitee.feizns.dynamic.reflect.Constructors;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取不通类型的客户端
 * @author feizns
 * @since 2024/3/19
 */
public abstract class Clients {

    /**
     * 客户端列表
     */
    private static final Map<Class<? extends Client>, Client> clients = new ConcurrentHashMap<>();

    /**
     * 获取指定类型的客户端
     * @return {@link T}
     */
    @SuppressWarnings("unchecked")
    public static <T extends Client> T get(Class<T> clientClass) {
        Client client = clients.getOrDefault(clientClass, create(clientClass));
        clients.put(clientClass, client);
        return (T) client;
    }

    /**
     * 创建一个客户端
     * @param clientClass {@link Client}
     * @return {@link T}
     */
    public static <T extends Client> T create(Class<T> clientClass) {
        return Constructors.newInstance(clientClass, httpsConfig());
    }

    /**
     * @return {@link com.aliyun.teaopenapi.models.Config}
     */
    public static com.aliyun.teaopenapi.models.Config httpsConfig() {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return config;
    }

}
